//
//  LeftViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/3/30.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "LeftViewController.h"
#import "LikeViewController.h"
#import "CollectionViewController.h"
#import "DownLoadViewController.h"
#import "SuggestViewController.h"
@interface LeftViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *iconNameArray;

@end

@implementation LeftViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"suixin_bg"]];
    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"我的关注",@"我的收藏",@"我的下载",@"清理缓存",@"反馈建议", nil];
    self.iconNameArray = [[NSMutableArray alloc] initWithObjects:@"like_",@"collect_",@"download_",@"setting_",@"fankui_", nil];
    
    [self createTableView];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/3)];
    headerView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headerView;
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:self.iconNameArray[indexPath.row]];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
    if (indexPath.row == 0) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.leftSlideVC closeLeftView];
        
        LikeViewController *likeVC = [[LikeViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:likeVC];
        [self.view.window.rootViewController presentViewController:nav animated:YES completion:nil];
    }
    
    if (indexPath.row == 1) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.leftSlideVC closeLeftView];
        
        CollectionViewController *collectionVC = [[CollectionViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:collectionVC];
        [self.view.window.rootViewController presentViewController:nav animated:YES completion:nil];
    }
    
    if (indexPath.row == 2) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.leftSlideVC closeLeftView];
        
        [self.view.window.rootViewController presentViewController:[DownLoadViewController shareDownLoad] animated:YES completion:nil];
    }
    
    if (indexPath.row == 3) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.leftSlideVC closeLeftView];
        
        [self clean];
    }
    
    if (indexPath.row == 4) {
        
        SuggestViewController *suggestVC = [[SuggestViewController alloc] init];
        
        [UIView animateWithDuration:0 animations:^{
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate.leftSlideVC closeLeftView];
            [self.view.window.rootViewController presentViewController:[[UINavigationController alloc] initWithRootViewController:suggestVC] animated:YES completion:nil];
        } completion:nil];
    }
}

- (void)clean{
    NSString *sizeStr = [NSString stringWithFormat:@"%fM",[self getCacheSize]];
    
    UIAlertController *actionsheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:sizeStr preferredStyle:UIAlertControllerStyleActionSheet];
    [actionsheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //删除按钮
        //1.删除sd
        [[SDImageCache sharedImageCache] clearMemory];//清除内存缓存
        [[SDImageCache sharedImageCache] clearDisk];//磁盘
        
    }]];
    [actionsheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }]];
    [self.view.window.rootViewController presentViewController:actionsheet animated:YES completion:nil];
}


- (CGFloat)getCacheSize {
    //缓存 有两类 sdwebimage 还有 每个界面保存的缓存
    CGFloat sdSize = [[SDImageCache sharedImageCache] getSize];
    NSString *myPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
    //获取文件夹中的所有的文件
    NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myPath error:nil];
    unsigned long long size = 0;
    for (NSString *fileName in arr) {
        NSString *filePath = [myPath stringByAppendingPathComponent:fileName];
        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        size += dict.fileSize;
    }
    //1M = 1024 K = 1024*1024字节
    CGFloat totalSize = (sdSize+size)/1024.0/1024.0;
    return totalSize;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
